#include "Stulpeliai.h"

void Stulpeliai::draw(HDC hdc)
{
	Bitmap bmp(width,height);
	Graphics *fromImg = Graphics::FromImage(&bmp);


	//SolidBrush  brush(Color(255, 0, 0, 255));
	//SolidBrush  brush2(Color(255, 255, 200, 000));
	

	SolidBrush  brushStulpeliai(Color(255, 120, 120, 120));
	SolidBrush  brushBackground(Color(255, 34, 34, 34));
	Pen			penBorder(Color(255, 0, 0, 0));


	fromImg->DrawRectangle(&penBorder, 0, 0, width-1, height-1);
	fromImg->FillRectangle(&brushBackground, 1, 1, width-2, height-2);
	//graphics.DrawRectangle(&pen, 5, 113, 250, 8);
	
	//int drawmode = 2;
	//drawMode = 1;

	float heightMultiplier = (float)(height-1) / 100;

	for(int i = 0; i < count-1; i++)
	{// max height 102
		//SolidBrush  zalia(Color(255, 255*heightData[i], 180, 255-255*heightData[i]));
		switch(this->drawMode)
		{
		case 0:
			{
				fromImg->FillRectangle(&brushStulpeliai, 2 + (i*4), height-(int)((heightData[i]*100)*heightMultiplier) -1  , 3, (int)((heightData[i]*100)*heightMultiplier));
				break;
			}

		case 1:
			{
				//if(i%2 == 0)
				fromImg->FillRectangle(&brushStulpeliai, 2 + (i*4), height-(int)((heightData[i]*100)*heightMultiplier) -1  , 3, (int)((heightData[i]*100)*heightMultiplier));
				//else
				fromImg->FillRectangle(&brushStulpeliai, 2 + (i*4), 1  , 3, (int)((heightData[i]*100)*heightMultiplier));
				break;
			}

		case 2:
			{

				fromImg->FillRectangle(&brushStulpeliai, 2 + (i*4), height/2-(int)((heightData[i]*50)*heightMultiplier)  , 3, (int)((heightData[i]*50)*heightMultiplier));
				fromImg->FillRectangle(&brushStulpeliai, 2 + (i*4), height/2  , 3, (int)((heightData[i]*50)*heightMultiplier));
				break;
			}

		case 3:
			{
				fromImg->FillRectangle(&brushStulpeliai, 2 + (i*4), height-(int)((heightData[i]*50)*heightMultiplier) -1  , 3, (int)((heightData[i]*50)*heightMultiplier));
				fromImg->FillRectangle(&brushStulpeliai, 2 + (i*4), 1  , 3, (int)((heightData[i]*50)*heightMultiplier));
				/*
				if(i%2 == 0)
				fromImg->FillRectangle(&zalia, 2 + (i*4), height-(int)(heightData[i]*50) -1  , 3, (int)(heightData[i]*100));
				else
				fromImg->FillRectangle(&zalia, 2 + (i*4), 1  , 3, (int)(heightData[i]*50));*/

				break;
			}

		case 4:
			{

				fromImg->FillRectangle(&brushStulpeliai, 2 + (i*4), height/2-(int)((heightData[i]*50)*heightMultiplier)  , 3, (int)((heightData[i]*50)*heightMultiplier));
				fromImg->FillRectangle(&brushStulpeliai, 2 + (i*4), height/2  , 3, (int)((heightData[i]*50)*heightMultiplier));
				break;
			}

			break;
		}
		
	}
   

	WCHAR *txt = new WCHAR[10];
	_stprintf(txt, _T("%d:%02d:%02d"), playingTime / 1000 / 60 / 60, playingTime / 1000 / 60 % 60, playingTime / 1000 % 60);
	fromImg->DrawString(txt, -1, &Font(&FontFamily(L"Arial"), 60, FontStyleRegular, UnitPixel), PointF(30, 0), &SolidBrush(Color(150, 204, 204, 204)));
	delete[] txt;

	//fromImg->FillRectangle(&zalia, 5, 113, 250, 8);
	
	Graphics    graphics(hdc);
	graphics.DrawImage(&bmp, x, y, width, height);

	delete fromImg;
}

void Stulpeliai::update(float *fl, int time)
{
	playingTime = time;
	int t = timeGetTime();
	//float *sum = new float[count];
	
	if(drawMode != 4)
	{
		int index = 0;
		for(int i = 0; i < count; i++)
		{
			float suma = 0;
			for(int j = 0; j < 1; j++)
			{
				suma += fl[index];
				index++;
			}
			//suma = 0.1;
		
			float sk = (float)suma*(i+1)*0.8;//  /player->getVolume()*5;
			if(sk > 1.0) sk = 1.0;


			heightData[i] = heightData[i] - (0.0015 * (t-lastTime));

			if(heightData[i] < 0) heightData[i] = 0.0;
			if(heightData[i] > 1) heightData[i] = 1.0;
		

			if(sk > heightData[i])
			heightData[i] = sk;



			//heightData[i] = 1;
		
		}
	}
	else
	{
		float sm = 0;
		for(int i = 1; i < 64; i ++)
		{
			heightData[i-1] = heightData[i];
			sm += fl[i-1];
		}
		
		sm = sm / 2;
		if(sm > 1.0) sm = 1.0;

		heightData[63] = sm;
	}

	RECT* lpRect = new RECT;

	lpRect->top = y;
	lpRect->left = x; 

	lpRect-> bottom = y+height;
	lpRect->right = x+width;


	InvalidateRect(hWnd, lpRect, false);

	delete[] lpRect;


	// stulpu redrawas
	/*for(int i = 0; i < 16; i++)
	{
		SendMessage(eqbar[i], PBM_SETPOS, ft[i]*100, 0);
	}*/

	//delete[] sum;
	lastTime = t;
}



int Stulpeliai::click()
{
	LPPOINT p = new POINT;
	GetCursorPos(p);
	ScreenToClient(hWnd, p);

	//drawMode++;
		//if(drawMode >= drawModeCount) drawMode = 0;
	if(p->x >= x+1 && p->x <= x + width-1 &&
		p->y >= y && p->y <= y + height)
	{
		drawMode++;
		if(drawMode >= drawModeCount) drawMode = 0;

	}

	delete[] p;
	return drawMode;
}
void Stulpeliai::setDrawMode(int m)
{
	if(m < 0) m = 0;
	if(m > drawModeCount) m = drawModeCount;
	drawMode = m;
}